#import "BWLayerWindowController.h"
#import "BWGraphDocument.h"
#import "BWLayerSet.h"
#import "BWTool.h"
#import "BWTableView.h"

#import "BWBackStitchLayer.h"
#import "BWCrossStitchLayer.h"

static BWLayerWindowController *g_controller;

@interface BWLayerWindowController(ForwardReferences)

- (void) toolChangeNotification: (NSNotification *) notification;

@end // ForwardReferences

@implementation BWLayerWindowController

- (void) showAffectedLayers
{
    NSArray *arrangedLayers = [layersController arrangedObjects];

    BWTool *tool = [[self document] valueForKey: @"currentTool"];
    NSArray *affectedLayers = [tool affectedLayers];

    NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
    
    int i;
    for (i = 0; i < [arrangedLayers count]; i++) {
        BWLayer *layer = [arrangedLayers objectAtIndex: i];
        if ([affectedLayers containsObject: layer]) {
            [indexes addIndex: i];
        }
    }
    [layerTableView setRowHighlightColor: [NSColor greenColor]];
    [layerTableView setRowHighlightIndexes: indexes];

    [indexes release];
    [layerTableView setNeedsDisplay: YES];

} // showAffectedLayers


- (void) setDocument: (NSDocument *) document
{
    [super setDocument: document];
    
    [self willChangeValueForKey: @"layers"];
    layers = [document valueForKey: @"layers"];
    [self didChangeValueForKey: @"layers"];
    [self showAffectedLayers];

    if (document == nil) {
        [[self window] setTitle: @"Layer Inspector"];
    }
    
} // setDocument


- (NSString *) windowTitleForDocumentDisplayName: (NSString *) displayName
{
    NSString *string;

    string = [NSString stringWithFormat: @"Layers for %@", displayName];
    return (string);

} // windowTitleForDocumentDisplayName



+ (NSString *) autosaveName
{
    return (@"layerWindow");
} // autosaveName


+ (BWWindowController *) sharedInstance
{
    return (g_controller);
} // sharedInstance


+ (void) setSharedInstance: (BWWindowController *) controller
{
    assert ([[controller class] isSubclassOfClass: self]);

    g_controller = (BWLayerWindowController *)controller;
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    [center addObserver: g_controller
            selector: @selector(toolChangeNotification:)
            name: BWGraphDocument_ToolChangeNotification
            object: nil];

    [(NSPanel*)[g_controller window] setBecomesKeyOnlyIfNeeded: YES];

} // setShraedInstance


+ (NSString *) windowNibName
{
    return (@"BWLayerWindow");
} // windowNibName



- (IBAction) showAllLayers: (id) sender
{
    [layers showAllLayers];

}  // showAllLayers


- (IBAction) soloLayer: (id) sender
{
    int row = [layerTableView selectedRow];
 
   if (row != -1) {
        [layers soloLayerAtIndex: row];
    }

} // soloLayer


+ (BWLayerWindowController *) sharedController
{
    return ((BWLayerWindowController*)[super sharedController]);
} // sharedController


- (void) toolChangeNotification: (NSNotification *) notification
{
    BWGraphDocument *doc = [notification object];

    if (doc == [self document]) {
        [self showAffectedLayers];
    }

} // toolChangeNotification


- (IBAction) addBackStitchLayer: (id) sender
{
    [[self document] addBackStitchLayer: sender];

} // addBackStitchLayer


- (IBAction) addCrossStitchLayer: (id) sender
{
    [[self document] addCrossStitchLayer: sender];
} // addCrossStitchLayer


- (IBAction) removeLayer: (id) sender
{
    int selectedRow;
    selectedRow = [layerTableView selectedRow];

    if (selectedRow != -1) {
        [layers removeLayerAtIndex: selectedRow];
    }

} // removeLayer


- (BOOL) validateMenuItem: (id <NSMenuItem>) menuItem
{
    BOOL result = YES;

    int selectedRow;
    selectedRow = [layerTableView selectedRow];

    // these guys need one layer selected
    if ([menuItem action] == @selector(soloLayer:)) {
        if (selectedRow == -1) {
            result = NO;
        }
    }
    
    if ([menuItem action] == @selector(removeLayer:)) {
        result = NO;

        if (selectedRow != -1) {
            // only nuke cross stitch and back stitch layers
            BWLayer *layer = [layers layerAtIndex: selectedRow];

            if ([layer layerKind] == kLayerKind_CrossStitch
                || [layer layerKind] == kLayerKind_BackStitch) {

                // don't nuke the last one
                if ([layers countOfLayersOfKind: [layer layerKind]] > 1) {
                    result = YES;
                }
            }
        }
    }

    return (result);

} // validateMenuItem



@end // BWLayerWindowController

